﻿using Biotech.Utils;
using MiniExcelLibs;
using project.B12.IService;
using project.B12.Model.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static project.B12.Model.Models.MicroplateMes;

namespace project.B12.Service
{
    /// <summary>
    /// conf 文件夹下所有的微孔板信息
    /// </summary>
    public class MICPlateMes: IMICPlateMes
    {
        public List<MicroplateMes> microplateAntibioticConcentrationMes { get; set; }

        public MICPlateMes()
        {
            Init();
        }

        public void Init()
        {
            if (!Directory.Exists("conf"))
                throw new Exception($"Directory 'conf' not found.文件缺失或异常");

            microplateAntibioticConcentrationMes = new List<MicroplateMes>();
            DirectoryInfo folder = new DirectoryInfo("conf");
            FileInfo[] fils = folder.GetFiles("*.xlsx");
            try
            {
                foreach (FileInfo file in fils)
                {
                    if (file.Name.Contains("$"))
                    {//缓存文件
                        continue;
                    }
                    MicroplateMes microplateM = new MicroplateMes(file.Name.Split('.')[0]);
                    var table = MiniExcel.QueryAsDataTable(file.FullName, useHeaderRow: true);
                    microplateM.antibiotic_concentration = new List<Layout>();
                    for (int i = 0; i < 8; i++)
                    {
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 0, Antibiotic = table.Rows[i].ItemArray[1].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 1, Antibiotic = table.Rows[i].ItemArray[2].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 2, Antibiotic = table.Rows[i].ItemArray[3].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 3, Antibiotic = table.Rows[i].ItemArray[4].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 4, Antibiotic = table.Rows[i].ItemArray[5].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 5, Antibiotic = table.Rows[i].ItemArray[6].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 6, Antibiotic = table.Rows[i].ItemArray[7].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 7, Antibiotic = table.Rows[i].ItemArray[8].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 8, Antibiotic = table.Rows[i].ItemArray[9].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 9, Antibiotic = table.Rows[i].ItemArray[10].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 10, Antibiotic = table.Rows[i].ItemArray[11].ToString() });
                        microplateM.antibiotic_concentration.Add(new Layout { Index = i * 12 + 11, Antibiotic = table.Rows[i].ItemArray[12].ToString() });
                    }
                    microplateM.Message = table.Rows[8].ItemArray[1].ToString();
                    microplateM.Type = short.Parse(microplateM.Message = table.Rows[8].ItemArray[4].ToString());
                    microplateM.Map = short.Parse(microplateM.Message = table.Rows[9].ItemArray[4].ToString());
                    microplateM.Volume = microplateM.Volume = table.Rows[10].ItemArray[4].ToString();
                    microplateAntibioticConcentrationMes.Add(microplateM);
                }
            }
            catch
            {
                throw new Exception($"【xlsx 文件缺失或异常】");
            }
        }
    }
}
